﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microserve.TeleEye.BusinessServices;


using MenuBar = Microserve.TeleEye.Common.MenuBar;
using Microserve.TeleEye.Data;
using Microserve.TeleEye.Web.Utility;
using System.Xml;


namespace Microserve.TeleEye.Web
{
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MenuController controller = new MenuController();
            List<MenuBar.MenuItem> sidebarMenus = controller.GetPageMenu("default.aspx", false);

            //toolboxlist.Controls.Add(new Literal() { Text = MenuFactory.GenerateMenu(sidebarMenus) }); 
            XmlDocument config = new XmlDocument();
            config.Load("http://www.teleeye.com.ph/blog/?feed=xml");
            XmlNodeList list = config.SelectNodes("main/newsitems/news");
            StringBuilder builder = new StringBuilder();
            foreach (XmlNode newsItem in list)
            {
                builder.Append("<div class='featured'><div style='float:left;");
                if (!string.IsNullOrEmpty(newsItem.SelectSingleNode("image").InnerText))
                {
                    builder.Append("width:139px;");
                }
                
                builder.Append("'><span class='style_title'>" +
                        generateLink(newsItem, "") + newsItem.SelectSingleNode("title").InnerText
                        + "</span></a><br/><br/>" +
                        newsItem.SelectSingleNode("abstract").InnerText + "<br/><span>"
                        + generateLink(newsItem, "style_model") +
                        newsItem.SelectSingleNode("linktext").InnerText +
                        "</a></span>");
                if (!string.IsNullOrEmpty(newsItem.SelectSingleNode("imageBig").InnerText))
                {
                    builder.Append("<img src='" +
                            newsItem.SelectSingleNode("imageBig").InnerText +
                        "' />");
                }

                builder.Append("</div>" + generateLink(newsItem, ""));
                if ( !string.IsNullOrEmpty(newsItem.SelectSingleNode("image").InnerText))
                {
                        builder.Append("<img src='" + 
                            newsItem.SelectSingleNode("image").InnerText +
                        "' style='width:100px;height:80px;'/>");
                }
                builder.Append("</a></div>");
            }
            this.Literal1.Text = builder.ToString();

            XmlDocument latestNews = new XmlDocument();
            latestNews.Load("http://www.teleeye.com.ph/blog/?feed=latest");
            XmlNodeList latestNewsList = latestNews.SelectNodes("main/newsitems/news");
            StringBuilder latestNewsBuilder = new StringBuilder();
            latestNewsBuilder.Append("<ul>");
            foreach (XmlNode newsItem in latestNewsList)
            {
                latestNewsBuilder.Append("<li>" + 
                    generateLink(newsItem, null ) +
                    "<strong>" + newsItem.SelectSingleNode("title").InnerText + 
                    "</strong></a></li>");
            }
            latestNewsBuilder.Append("</ul>");

            this.Literal2.Text = latestNewsBuilder.ToString();
        }

        private static string generateLink(XmlNode newsItem, string style)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<a href='");
            builder.Append(newsItem.SelectSingleNode("link").InnerText);
            builder.Append("' ");
            if (!string.IsNullOrEmpty(style))
            {
                builder.Append("class='");
                builder.Append(style);
                builder.Append("'");
            }
            builder.Append(" target='");
            builder.Append(newsItem.SelectSingleNode("target").InnerText);
            builder.Append("'>");
            return builder.ToString();
        }
    }
}
